//
//  SquareATableViewCell.m
//  安好
//
//  Created by rlp on 14-11-14.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SquareATableViewCell.h"

@implementation SquareATableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.title = [[UILabel alloc]init];
//        [self.contentView addSubview:self.title];
        
        self.content = [[UILabel alloc]init];
//        [self.contentView addSubview:self.content];
        self.squareCellImageView = [[UIView alloc]init];
//        [self.contentView addSubview:self.squareCellImageView];
        
        self.timesLabel = [[UILabel alloc]init];
//        [self.contentView addSubview:self.timesLabel];
        self.bottomView = [[UIView alloc]init];
//        [self.contentView addSubview:self.bottomView];
        
        self.arrayNumber = [[NSArray alloc]init];
        
        [self setNeedsDisplay];

    }
    return self;
}


-(void)loadViewSquareMarkStyles:(NSArray *)squareMarkStyles{
    
    
    
    
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    
    self.squareMarkView = [[SquareMarkView alloc]initWithFrame:CGRectMake(0, 15, 0, 0)];
    self.squareMarkView.squareMarkStyles = squareMarkStyles;
    [self.contentView addSubview:self.squareMarkView];
    
    
    if (self.title.text==nil||self.title.text.length==0||[self.title.text isEqual:[NSNull null]]) {
        self.title.text=@" ";
    }
    CGRect titlefram =CGRectMake(15, 12, kScreenWidth-15*2-self.squareMarkView.frame.size.width, 0);
    UILabel *tempLabel = [[UILabel alloc]initWithFrame:titlefram text:self.title.text font:[UIFont systemFontOfSize:TITLE_TEXTLABEL_FOUNT_SIZE] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:5.0f];

    [self.title removeFromSuperview];
    if (tempLabel.frame.size.height>=15*2+3*2+5*1) {
       self.title =[[UILabel alloc]initWithFrame:titlefram text:self.title.text font:[UIFont systemFontOfSize:TITLE_TEXTLABEL_FOUNT_SIZE] textColor:TITLE_ARTICLE_TEXTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:2 lineSpacing:5.0f];
    }else{
        self.title=tempLabel;
    }
    [self.contentView addSubview:self.title];

    float titleHight = self.title.frame.size.height+self.title.frame.origin.y;
    
    
    
    
    if (self.content.text==nil||self.content.text.length==0||[self.content.text isEqual:[NSNull null]]) {
        self.content.text=@" ";
    }

    CGRect contentfram =CGRectMake(15, self.title.frame.size.height+self.title.frame.origin.y+10, kScreenWidth-15*2, 0);
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:contentfram text:self.content.text font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13] textColor:TITLE_ARTICLE_CONTENTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:3 lineSpacing:5.0f];
    contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    
    [self.content removeFromSuperview];
    if (tempLabel.frame.size.height>=13*3+3*3+5*2) {
        self.content =[[UILabel alloc]initWithFrame:contentfram text:self.content.text font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13] textColor:TITLE_ARTICLE_CONTENTFONT_COLOR backgroundColor:[UIColor clearColor] numberOfLines:3 lineSpacing:5.0f];
    }else{
        self.content=contentLabel;
    }
    [self.contentView addSubview:self.content];

    float contentHight=0;
    contentHight=self.content.frame.size.height+10;
    
    
    
    
    
    
    


    


    
    
    
    
    
    float squareCellImageViewsHight=0;
    if (self.squareCellImageViewsArray==nil||self.squareCellImageViewsArray.count==0) {
        squareCellImageViewsHight=0;
        [self.squareCellImageView removeFromSuperview];

        
    }else{
        
        
        for (UIView *view in [self.squareCellImageView subviews]) {
            [view removeFromSuperview];
        }
        self.squareCellImageView.frame=CGRectMake(15, titleHight+contentHight+10, kScreenWidth-15*2, 55);
        
        [self.contentView addSubview:self.squareCellImageView];
        
        squareCellImageViewsHight=self.squareCellImageView.frame.size.height+10;

        for (int i=0; i<(self.squareCellImageViewsArray.count>=4?4:self.squareCellImageViewsArray.count); i++) {
            
            UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake((55+23
                                                                                   )*i, 0, 55, 55)];
            imageview.tag = i;
            [imageview setImageURLStr:[NSString stringWithFormat:@"%@",[self.squareCellImageViewsArray objectAtIndex:i]] placeholder:[UIImage imageNamed:@"公用_默认图片"]];
            imageview.layer.masksToBounds = YES;
            imageview.layer.cornerRadius = 3.0;
            imageview.userInteractionEnabled = YES;
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClicked:)];
            [imageview addGestureRecognizer:singleTap];
            [self.squareCellImageView  addSubview:imageview];
            
        }
        
    }
    for (UIView *view in [self.bottomView subviews]) {
        [view removeFromSuperview];
    }
//    [self.bottomView removeFromSuperview];
    self.bottomView.frame =CGRectMake(15, titleHight+contentHight+squareCellImageViewsHight+10, kScreenWidth-15*2, 12);
    self.bottomView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.bottomView];
    
    [self.timesLabel removeFromSuperview];
    
    
    self.timesLabel.frame =CGRectMake(0, 0, self.bottomView.frame.size.width, ((kScreenWidth == 414)?12+2:12));
    self.timesLabel.textAlignment =NSTextAlignmentRight;
    self.timesLabel.textColor = TIME_COLOR;
    
    self.timesLabel.backgroundColor = [UIColor clearColor];
    self.timesLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_TIME];
    [self.timesLabel sizeToFit];
    self.timesLabel.frame =CGRectMake(self.bottomView.size.width-self.timesLabel.frame.size.width, self.timesLabel.frame.origin.y, self.timesLabel.frame.size.width, 12);

    [self.bottomView addSubview:self.timesLabel];
    
    
    
    
    UIImageView *viewTime = [[UIImageView alloc]initWithFrame:CGRectMake(self.bottomView.frame.size.width-self.timesLabel.frame.size.width-15, self.timesLabel.frame.origin.y, ((kScreenWidth == 414)?12+2:12), ((kScreenWidth == 414)?12+2:12))];
    viewTime.image=[UIImage imageNamed:@"广场_时间"];
    [self.bottomView addSubview:viewTime];
    
    
    
    
    
    
    

    
    NSArray *array = [NSArray arrayWithObjects:@"广场_收藏",@"广场_赞",@"广场_评论", nil];
//    NSArray *arrayNumber = [NSArray arrayWithObjects:@" 11",@" 1332",@" 12312", nil];

    for (int i=0; i<3; i++) {
        UIButton *button = [[UIButton alloc]init];
        
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",[array objectAtIndex:i]]] forState:UIControlStateNormal];
        [button setTitle:[NSString stringWithFormat:@" %@",[self.arrayNumber objectAtIndex:i]] forState:UIControlStateNormal];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(1.0, -4.0, -1.0, 4.0)];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0.0, -4.0, 0.0, 4.0)];

        button.tag = i+BUTTONADD_TAG;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:10.f]};

        CGSize size = [[NSString stringWithFormat:@" %@",[self.arrayNumber objectAtIndex:i]] boundingRectWithSize:CGSizeMake(MAXFLOAT, 12) options:NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil].size;
        
        
        [button setTitleColor:TIME_COLOR forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_TIME];
        
        
        if (i==0) {
            
            button.frame = CGRectMake(0, 0, size.width+20, size.height);

            
        }else{
            UIButton *btn1 = (UIButton *)[self.bottomView viewWithTag:(i-1+BUTTONADD_TAG)];
            button.frame = CGRectMake(btn1.frame.size.width+btn1.frame.origin.x, 0, size.width+20, size.height);
        }
        [self.bottomView addSubview:button];

        
//        MyNSLog("%f,%f,%f,%f", button.frame.origin.x,button.frame.origin.y,button.frame.size.width,button.frame.size.height);

    }

    
    float bottomViewHight = self.bottomView.frame.size.height+10;
    
    

    
    
    self.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,kScreenWidth,titleHight+contentHight+squareCellImageViewsHight+bottomViewHight+self.title.frame.origin.y);

    
    
    


    
    /**
     *  线条的处理
     */
    
    UIView *bottomLineView = [[UIView alloc]initWithFrame:CGRectMake(5, self.frame.size.height-1,kScreenWidth-5, 0.5)];
    
    bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    [self.contentView addSubview:bottomLineView];

}
-(IBAction)buttonClick:(id)sender{
    NSInteger tags = [(UIButton *)sender tag]-10;
    [self.delegate bottomViewIndex:[(UIButton *)sender superview].tag subImageIndex:tags];
}
-(IBAction)imageClicked:(id)sender{
    UITapGestureRecognizer *top = sender;
    NSInteger tags = top.view.tag;
    [self.delegate squareCellImageViewsIndex:top.view.superview.tag subImageIndex:tags];
    
}
-(void)setSquareCellImageViewsArray:(NSMutableArray *)squareCellImageViewsArray{
    _squareCellImageViewsArray = squareCellImageViewsArray;
}
-(void)setSquareMarkStyles:(NSArray *)squareMarkStyles{
    _squareMarkStyles = squareMarkStyles;
    [self loadViewSquareMarkStyles:_squareMarkStyles];
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context, rect);
    
//    //上分割线，
//    CGContextSetStrokeColorWithColor(context, [ClassMethodsViewController colorWithHexString:@"#e2e2e2"].CGColor);
//    CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 1));
    
    //下分割线
    CGContextSetStrokeColorWithColor(context, [ClassMethodsViewController colorWithHexString:@"#e2e2e2"].CGColor);
    CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 1));
}

@end
